package com.davidniu.lesson05;

import com.davidniu.lesson05.utils.JdbcUtils_C3P0;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class TestC3P0 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = JdbcUtils_C3P0.getConnection();
            //使用?占位符代替参数
            String sql =  "INSERT INTO users (`id`,`NAME`,`PASSWORD`,`email`,`birthday`) VALUES(?,?,?,?,?)";
            stmt = conn.prepareStatement(sql);//预编译sql,先写sql,不执行
            //手动给参数赋值
            stmt.setInt(1, 5);
            stmt.setString(2, "Scott");
            stmt.setString(3, "123456");
            stmt.setString(4,"dd@163.com");
            // 两个Date： java.util.Date 和 java.sql.Date
            stmt.setDate(5, new java.sql.Date(new Date().getTime()) );
            //执行
            int i = stmt.executeUpdate();
            if(i>0) {
                System.out.println("插入成功！");
            }
        } catch(SQLException e){
            e.printStackTrace();
        } finally {
            JdbcUtils_C3P0.release(conn, stmt, rs);
        }
    }
}
